//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.sunmi.sbs.commons.utils;

import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtils {
    public HttpClientUtils() {
    }

    public static JSONObject httpclient(String host, String path, JSONObject headers, JSONObject payload) throws URISyntaxException, IOException {
        JSONObject resp = new JSONObject();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        URI uri = (new URIBuilder()).setScheme("https").setHost(host).setPath(path).build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/json");
        String key;
        if (headers != null && !headers.isEmpty()) {
            Iterator var8 = headers.keySet().iterator();

            while(var8.hasNext()) {
                key = (String)var8.next();
                boolean contentTypeIsExist = "Content-Type".equals(key);
                if (!contentTypeIsExist) {
                    String value = headers.getString(key);
                    request.setHeader(key, value);
                }
            }
        }

        request.setEntity(new StringEntity(StringEscapeUtils.unescapeJava(String.valueOf(payload))));
        CloseableHttpResponse response = httpclient.execute(request);
        if (response.getStatusLine().getStatusCode() == 200) {
            key = EntityUtils.toString(response.getEntity(), "UTF-8");
            resp = JSONObject.parseObject(key);
        }

        return resp;
    }
}
